package cn.iocoder.yudao.module.system.controller.admin.pay;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.system.controller.admin.pay.vo.*;
import cn.iocoder.yudao.module.system.service.pay.SystemPayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.math.BigDecimal;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 发起支付")
@RestController
@RequestMapping("/system/pay")
@Validated
@Slf4j
public class SystemPayController {

    @Resource
    private SystemPayService systemPayService;

    @PostMapping("/confirm")
    @Operation(summary = "确认支付订单")
    public CommonResult<SystemPayConfirmRespVO> confirmPay(@RequestBody SystemPayConfirmReqVO reqVO) {
        return success(systemPayService.confirmPay(reqVO));
    }

    @PostMapping("/confirm-renewal")
    @Operation(summary = "确认支付订单")
    public CommonResult<SystemPayConfirmRespVO> confirmRenewalPay(@RequestBody SystemPayRenewalConfirmReqVO reqVO) {
        return success(systemPayService.confirmRenewalPay(reqVO));
    }

    @GetMapping("/get-pre-order")
    @Operation(summary = "获取订单")
    public CommonResult<SystemPayConfirmRespVO> getPreOrder(@RequestParam("preOrderNo") String preOrderNo) {
        return success(systemPayService.getPreOrderNo(preOrderNo));
    }

    @GetMapping("/get-order-is-paid")
    @Operation(summary = "获取支付订单是否完成支付")
    public CommonResult<Boolean> getOrder(@RequestParam("preOrderNo") String preOrderNo) {
        return success(systemPayService.getOrderIsPaid(preOrderNo));
    }

    @PostMapping("/create")
    @Operation(summary = "创建支付订单")
    public CommonResult<Long> createPay(@RequestBody SystemPayCreateReqVO callPayCreateReqVO) {
        return success(systemPayService.createPay(callPayCreateReqVO));
    }

    @PostMapping("/wallet")
    @Operation(summary = "钱包充值")
    public CommonResult<SystemPayConfirmRespVO> rechargeWallet(@RequestBody SystemWalletRechargeReqVO reqVO) {
        if (reqVO.getRechargeValue().compareTo(BigDecimal.ZERO) < 0) {
            error(100500, "充值金额不能小于0");
        }
        return success(systemPayService.createWalletRecharge(reqVO));
    }


    // TODO: 单个续费的支付逻辑 （skuId）
//    @PostMapping("/renewal")
//    @Operation(summary = "续费单个产品")
//    public CommonResult<SystemPayConfirmRespVO> rechargeWallet(@RequestBody SystemWalletRechargeReqVO reqVO) {
//        if (reqVO.getRechargeValue().compareTo(BigDecimal.ZERO) < 0) {
//            error(500, "充值金额不能小于0");
//        }
//        return success(systemPayService.createWalletRecharge(reqVO));
//    }
}
